package gvo.presets;

import gvo.util.GVOstore;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;

public class Presets {
	// fix values
	private String db_User = "sa";
	private String db_Pw = "";
	public static final int NOID = -1;
	public static final String EMPTYTXT = "";
	public static final int MAXLENGTH = 40;
	public static final int ZUSATZLENGTH = 6;
	//properties start
	private Properties properties;
	private int reroResultsPerPage = 20;
	private String dbPath = "/DB/";
	private boolean dbPathRelativ = true;
	private String startSuchfeld = "Name";
	private String startSortierfeld = "Name";
	private String startAnzeigefeld = "Name";
	private boolean isIntern = true;
	private boolean complSearch = false;
	private boolean showImage = true;
	private int delay = 1;
	//POSITIONS
	private int xGVOguiB = 370;
	private int yGVOguiB = 30;
	private int wGVOguiB = 750;
	private int hGVOguiB = 860;
	
	private int xGVOguiD = 450;
	private int yGVOguiD = 20;
	private int wGVOguiD = 750;
	private int hGVOguiD = 600;
	
	private int xImportW = 55;
	private int yImportW = 22;
	private int wImportW = 400;
	private int hImportW = 500;
	
	private int xImageViewer = 1040;
	private int yImageViewer = 370;
	//properties end
	
	public Presets(){
		if(readProperties()){
			setReroResultsPerPage(Constants.getReroResultsPerPage(getProperties()));
			setDbPath(Constants.getDbPath(getProperties()));
			setDbPathRelativ(Constants.getDbPathRelativ(getProperties()));
			setStartAnzeigefeld(Constants.getStartAnzeigefeld(getProperties()).trim().toUpperCase());
			setStartSortierfeld(Constants.getStartSortierfeld(getProperties()).trim().toUpperCase());
			setStartSuchfeld(Constants.getStartSuchfeld(getProperties()).trim().toUpperCase());
			setIntern(Constants.getIsIntern(getProperties()));
			setComplSearch(Constants.getComplSearch(getProperties()));
			setDelay(Constants.getDelay(getProperties()));
			setShowImage(Constants.getShowImage(getProperties()));
			setXGVOguiB(Constants.getGVOGUIB(getProperties()));
			setYGVOguiB(Constants.getYGVOGUIB(getProperties()));
			setWGVOguiB(Constants.getWGVOGUIB(getProperties()));
			setHGVOguiB(Constants.getHGVOGUIB(getProperties()));
			setXGVOguiD(Constants.getXGVOGUID(getProperties()));
			setYGVOguiD(Constants.getYGVOGUID(getProperties()));
			setWGVOguiD(Constants.getWGVOGUID(getProperties()));
			setHGVOguiD(Constants.getHGVOGUID(getProperties()));
			setXImportW(Constants.getXImportW(getProperties()));
			setYImportW(Constants.getYImportW(getProperties()));
			setWImportW(Constants.getWImportW(getProperties()));
			setHImportW(Constants.getHImportW(getProperties()));
			setXImageViewer(Constants.getXImageViewer(getProperties()));
			setYImageViewer(Constants.getYImageViewer(getProperties()));
		} else {
			this.setReroResultsPerPage(20);
			this.setDbPath("/DB/");
			this.setDbPathRelativ(true);
		}
	}
	public void setPropertiesFromValues(){
		Constants.setComplSearch(properties, complSearch);
		Constants.setDbPath(properties, dbPath);
		Constants.setDbPathRelativ(properties, dbPathRelativ);
		Constants.setDelay(properties, delay);
		Constants.setIsIntern(properties, isIntern);
		Constants.setReroResultsPerPage(properties, reroResultsPerPage);
		Constants.setShowImage(properties, showImage);
		Constants.setStartAnzeigefeld(properties, startAnzeigefeld);
		Constants.setStartSortierfeld(properties, startSortierfeld);
		Constants.setStartSuchfeld(properties, startSuchfeld);
		Constants.setHGVOGUIB(properties, hGVOguiB);
		Constants.setWGVOGUIB(properties, wGVOguiB);
		Constants.setXGVOGUIB(properties, xGVOguiB);
		Constants.setYGVOGUIB(properties, yGVOguiB);
		Constants.setHGVOGUID(properties, hGVOguiD);
		Constants.setWGVOGUID(properties, wGVOguiD);
		Constants.setXGVOGUID(properties, xGVOguiD);
		Constants.setYGVOGUID(properties, yGVOguiD);
		Constants.setHImportW(properties, hImportW);
		Constants.setWImportW(properties, wImportW);
		Constants.setXImportW(properties, xImportW);
		Constants.setYImportW(properties, yImportW);
		Constants.setXImageViewer(properties, xImageViewer);
		Constants.setYImageViewer(properties, yImageViewer);
	}
	private boolean readProperties() {
		FileInputStream fis = null; 
		boolean res = false;
		try {
            this.setProperties(new Properties());
            fis = new FileInputStream("properties/biblio.properties");
            this.getProperties().load(fis);
            res = true;
        }
        catch (IOException e) {
        		GVOstore.showMessage("no property file found, nothing interesting will happen", Level.WARNING, GVOstore.class.getName());
        }
        try {
			fis.close();
		} catch (IOException e) {
			GVOstore.showMessage("File kann nicht geschlosen werden! ", Level.WARNING, GVOstore.class.getName());
		}
        return res;
    }
	public boolean writeProperties() {
		FileOutputStream out = null;
		boolean res = true;
		try {
			out = new FileOutputStream("properties/biblio.properties");
			this.getProperties().store(out, "Bibliothekseinstellungen");
			
		} catch (IOException e) {
			GVOstore.showMessage("das File 'properties/biblio.properties' konnte nicht erstellt werden!", Level.WARNING, GVOstore.class.getName());
			res = false;
		} finally {
			try{
				if (out != null) {
					//flush and close "in" and its underlying streams
					out.close();
			    }
			} catch (IOException ex){
				GVOstore.showMessage("das File 'properties/biblio.properties' konnte nicht geschlossen werden! "+ex, Level.WARNING, GVOstore.class.getName());
			}
		}
		return res;
	}
	public Properties getProperties() {
		return properties;
	}
	public void setProperties(Properties properties) {
		this.properties = properties;
	}
	public void setDb_User(String db_User) {
		this.db_User = db_User;
	}
	public String getDb_User() {
		return db_User;
	}
	public void setDb_Pw(String db_Pw) {
		this.db_Pw = db_Pw;
	}
	public String getDb_Pw() {
		return db_Pw;
	}
	public int getReroResultsPerPage() {
		return reroResultsPerPage;
	}
	public void setReroResultsPerPage(int reroResultsPerPage) {
		this.reroResultsPerPage = reroResultsPerPage;
	}
	public String getDbPath() {
		return dbPath;
	}
	public void setDbPath(String dbPath) {
		this.dbPath = dbPath;
	}
	public boolean isDbPathRelativ() {
		return dbPathRelativ;
	}
	public void setDbPathRelativ(boolean dbPathRelativ) {
		this.dbPathRelativ = dbPathRelativ;
	}
	public String getStartSuchfeld() {
		return startSuchfeld;
	}
	public void setStartSuchfeld(String startSuchfeld) {
		this.startSuchfeld = startSuchfeld;
	}
	public String getStartSortierfeld() {
		return startSortierfeld;
	}
	public void setStartSortierfeld(String startSortierfeld) {
		this.startSortierfeld = startSortierfeld;
	}
	public String getStartAnzeigefeld() {
		return startAnzeigefeld;
	}
	public void setStartAnzeigefeld(String startAnzeigefeld) {
		this.startAnzeigefeld = startAnzeigefeld;
	}
	public boolean isIntern() {
		return isIntern;
	}
	public void setIntern(boolean isIntern) {
		this.isIntern = isIntern;
	}
	public boolean isComplSearch() {
		return complSearch;
	}
	public void setComplSearch(boolean complSearch) {
		this.complSearch = complSearch;
	}
	public boolean isShowImage() {
		return showImage;
	}
	public void setShowImage(boolean showImage) {
		this.showImage = showImage;
	}
	public int getDelay() {
		return delay;
	}
	public void setDelay(int delay) {
		this.delay = delay;
	}
	public int getXGVOguiB() {
		return xGVOguiB;
	}
	public void setXGVOguiB(int oguiB) {
		xGVOguiB = oguiB;
	}
	public int getYGVOguiB() {
		return yGVOguiB;
	}
	public void setYGVOguiB(int oguiB) {
		yGVOguiB = oguiB;
	}
	public int getWGVOguiB() {
		return wGVOguiB;
	}
	public void setWGVOguiB(int oguiB) {
		wGVOguiB = oguiB;
	}
	public int getHGVOguiB() {
		return hGVOguiB;
	}
	public void setHGVOguiB(int oguiB) {
		hGVOguiB = oguiB;
	}
	public int getXGVOguiD() {
		return xGVOguiD;
	}
	public void setXGVOguiD(int oguiD) {
		xGVOguiD = oguiD;
	}
	public int getYGVOguiD() {
		return yGVOguiD;
	}
	public void setYGVOguiD(int oguiD) {
		yGVOguiD = oguiD;
	}
	public int getWGVOguiD() {
		return wGVOguiD;
	}
	public void setWGVOguiD(int oguiD) {
		wGVOguiD = oguiD;
	}
	public int getHGVOguiD() {
		return hGVOguiD;
	}
	public void setHGVOguiD(int oguiD) {
		hGVOguiD = oguiD;
	}
	public int getXImportW() {
		return xImportW;
	}
	public void setXImportW(int importW) {
		xImportW = importW;
	}
	public int getYImportW() {
		return yImportW;
	}
	public void setYImportW(int importW) {
		yImportW = importW;
	}
	public int getWImportW() {
		return wImportW;
	}
	public void setWImportW(int importW) {
		wImportW = importW;
	}
	public int getHImportW() {
		return hImportW;
	}
	public void setHImportW(int importW) {
		hImportW = importW;
	}
	public int getXImageViewer() {
		return xImageViewer;
	}
	public void setXImageViewer(int imageViewer) {
		xImageViewer = imageViewer;
	}
	public int getYImageViewer() {
		return yImageViewer;
	}
	public void setYImageViewer(int imageViewer) {
		yImageViewer = imageViewer;
	}

}
